<template>
    <div class="register">
        <el-container>
            <el-card class="form-card">
                <el-form :model="registerForm" ref="registerForm" :rules="rules">
                    <h3>登录</h3>
                    <el-form-item prop="userName">
                        <el-input placeholder="请输入用户名" v-model="registerForm.userName"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input placeholder="请输入密码" v-model="registerForm.userPassword" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('registerForm')">登录</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="register">注册</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </el-container>

    </div>
</template>

<script>
    export default {
        data() {
            return {
                registerForm: {
                    userName: '',
                    userPassword: ''
                },
                rules: {
                    userName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    userPassword: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const _this = this
                        axios.post('http://localhost:2020/login',this.registerForm)
                            .then(function (res) {
                            if (res.data.status === 200) {
                                _this.$message({
                                    showClose: true,
                                    message: res.data.msg,
                                    type: 'success',
                                    center: true
                                });
                                _this.$store.commit('login', res.data.object)
                                _this.$router.push('/note/noteshelf');
                            } else {
                                _this.$confirm(res.data.msg, '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning',
                                    center: true
                                })
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            register() {
                this.$router.push('/register')
            }
        }
    }
</script>

<style scoped>

    .el-container {
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #b598a1;
    }
    h3 {
        margin: 16px;
    }
    .form-card {
        width: 400px;
        background: #e0c8d1;
        border: none;
        box-shadow: 0 2px 12px 0 rgba(0,0,0,.5);
    }
    .el-button {
        width: 100%;
    }
</style>